package com.example.studentmanagersystem;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.Toast;

import com.fs.adapters.ClassesAdapter;
import com.fs.po.Classes;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class AdminClassesManageActivity extends Activity implements OnClickListener {

	private CheckBox deleteCheckBox;
	private ListView classesListView;
	
	private Button addClass,actionBack,actionMenu;
	
	private ClassesAdapter classAdapter;
	private List<Classes> classList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.admin_classes_manage_activity);
		this.init();
		this.createListaner();
		setData();
	}
	
	/**
	 * initialize
	 */
	private void init() {
		this.deleteCheckBox=(CheckBox) this.findViewById(R.id.admin_classes_manage_classesCheck);
		this.classesListView=(ListView) this.findViewById(R.id.admin_classes_manage_classesListView);
		
		this.addClass=(Button) this.findViewById(R.id.admin_classes_manage_addClass);
		
		this.actionBack=(Button) this.findViewById(R.id.classManage_action_backButton);
		
		this.actionMenu=(Button) this.findViewById(R.id.classManage_action_menu);
		
		this.addClass.setOnClickListener(this);
		this.actionBack.setOnClickListener(this);
		this.actionMenu.setOnClickListener(this);
		
		this.classAdapter=new ClassesAdapter(this,handler);
		this.classesListView.setAdapter(this.classAdapter);
		
		
	}
	
	/**
	 * set data which get from service
	 */
	public void setData() {
		
		this.getData("admin_get_classes");
		
	
	}
	
	
	/**
	 * set Listener
	 */
	private void createListaner() {
		//CheckBox
		this.deleteCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				
				 
			}
		});
		
	}
	
	
	/**
	 * get data method
	 */
	public void getData(String action) {
		
		RequestParams params = new RequestParams();
		
		params.addBodyParameter("action", action);
		
		HttpUtils httpUtils=new HttpUtils();
		
		httpUtils.send(HttpMethod.POST, URL.ClASS_URL, params, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {

				String jsonString=responseInfo.result;
				
				setClassData(jsonString);
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				
			}

		});
	}
	
	
	/**
	 * 解析数据
	 * @param jsonString
	 * @return
	 */
		private void setClassData(String jsonString) {
			
			classList=new ArrayList<Classes>();
				
			Classes cla=null;
				if(jsonString!=null){
					
					try {
						JSONObject jsonObject = new JSONObject(jsonString);
						JSONArray array=jsonObject.getJSONArray("class");
						for(int i=0;i<array.length();i++){
							JSONObject json=array.getJSONObject(i);
							int class_id=Integer.parseInt(json.getString("_id"));
							String class_name=json.getString("class_name");
							cla=new Classes(class_id, class_name);
							
							classList.add(cla);
						}
						
						System.out.println("List传值前："+classList.size());//【测试】
						
						this.classAdapter.setAdapterData(classList);
						this.classAdapter.notifyDataSetChanged();
						
					} catch (JSONException e) {
						e.printStackTrace();
					}
					
				}
				
				System.out.println("List封装后："+classList.size());
			}
		
	/**
	 * add class
	 */

	@Override
	public void onClick(View v) {
		
		int id=v.getId();
		 
		if(id==R.id.admin_classes_manage_addClass){
			
			Intent intent=new Intent(this,AdminAddClassActivity.class);
			this.startActivity(intent);
			
		}else if(id==R.id.classManage_action_backButton){
			
			this.finish();
			
			
		}else if(id==R.id.classManage_action_menu){
			
			PopupMenuUtil popupMenuUtil=new PopupMenuUtil(v,this);
			popupMenuUtil.actionBackListener();
			
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		setData();
		this.classAdapter.notifyDataSetChanged();
		
	}
	
	
	Handler handler=new Handler(){
		
		public void handleMessage(Message msg) {
			
			if(msg.what==2){
				
				setData();
			}
			
		}
		
	};
}
